package com.zxl.live.screen.ads;

import android.content.Context;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.zxl.live.screen.ads.listener.AdBannerAdapter;

/**
 * Created by Administrator on 2018/2/6.
 */

public class AdmobBannerHelper extends AdBannerAdapter {

    private boolean mIsRequestAds;

    protected AdView mAdView;

    public AdmobBannerHelper(String type, AdListener mProxy) {
        super(type, mProxy);
    }

    public void loadAds(Context context, AdSize adSize, String adId) {
        if (!mIsRequestAds) {
            mIsRequestAds = true;
            mAdView = new AdView(context);
            mAdView.setAdSize(adSize);
            mAdView.setAdListener(this);
            //ApplicationHelper.DEBUG ? "ca-app-pub-3940256099942544/6300978111" :
            mAdView.setAdUnitId(adId);
            //.addTestDevice("7169E3A2C2E2F538DABAC75B421385A3")
            mAdView.loadAd(new AdRequest.Builder().build());
        }
    }

    @Override
    public void onAdLoaded() {
        super.onAdLoaded();
        mIsRequestAds = false;
    }

    @Override
    public void onAdFailedToLoad(int i) {
        super.onAdFailedToLoad(i);
        mIsRequestAds = false;
    }

    public AdView getAdView() {
        return mAdView;
    }

    public void recycle() {
        if (null != mAdView) {
            ViewParent parent = mAdView.getParent();
            if (parent != null) {
                ((ViewGroup) parent).removeAllViews();
            }
            mAdView.destroy();
        }
    }
}
